<?php
/*  
Plugin Name: Jigoshop Second Flat Rate Shipping
Description: Add a second Flat Rate field to Jigoshop.
Author: SteeleTech.us
Version: 1.0
Author URI: http://www.steeletech.us

Copyright 2013  SteeleTech.us  (email : info@polevaultweb.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as 
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

add_action('plugins_loaded', 'steele_jigoshop_2ndFRS_load', 0);
function steele_jigoshop_2ndFRS_load() {
	if(! class_exists('jigoshop_shipping_method')) return;
	add_filter('jigoshop_shipping_methods', 'add_flat_rate2_method', 11);
	function add_flat_rate2_method($x) { $x[] = 'flat_rate2'; return $x; }
	class flat_rate2 extends jigoshop_shipping_method {
		public function __construct() {
			parent::__construct();
			$this->id			= 'flat_rate2';
			$this->enabled		= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_enabled');
			$this->title 		= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_title');
			$this->availability = Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_availability');
			$this->countries 	= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_countries');
			$this->type 		= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_type');
			$this->tax_status	= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_tax_status');
			$this->cost 		= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_cost');
			$this->fee 			= Jigoshop_Base::get_options()->get_option('jigoshop_flat_rate2_handling_fee');
			add_action('jigoshop_settings_scripts', array(&$this, 'admin_scripts'));
			}
		protected function get_default_options() {
			$defaults = array();
			$defaults[] = array('name' => __('Second Flat Rate', 'jigoshop'), 'type' => 'title', 'desc' => __('Flat rates let you define a standard rate per item, or per order.', 'jigoshop'));
			$defaults[] = array('name' => __('Enable Second Flat Rate','jigoshop'), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_flat_rate2_enabled', 'std' => 'yes', 'type' => 'checkbox', 'choices' => array('no' => __('No', 'jigoshop'), 'yes' => __('Yes', 'jigoshop')));
			$defaults[] = array('name' => __('Method Title','jigoshop'), 'desc' => '', 'tip' => __('This controls the title which the user sees during checkout.','jigoshop'), 'id' => 'jigoshop_flat_rate2_title', 'std' => __('Second Flat Rate','jigoshop'), 'type' => 'text');
			$defaults[] = array('name' => __('Type','jigoshop'), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_flat_rate2_type', 'std' => 'order', 'type' => 'radio', 'choices' => array('order' => __('Per Order', 'jigoshop'), 'item' => __('Per Item', 'jigoshop')));
			$defaults[] = array('name' => __('Tax Status','jigoshop'), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_flat_rate2_tax_status', 'std' => 'taxable', 'type' => 'radio', 'choices' => array('taxable' => __('Taxable', 'jigoshop'), 'none' => __('None', 'jigoshop')));
			$defaults[] = array('name' => __('Cost','jigoshop'), 'desc' => '', 'type' => 'decimal', 'tip' => __('Cost excluding tax. Enter an amount, e.g. 2.50.','jigoshop'), 'id' => 'jigoshop_flat_rate2_cost', 'std' => '0');
			$defaults[] = array('name' => __('Handling Fee','jigoshop'), 'desc' => '', 'type' => 'text', 'tip' => __('Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.','jigoshop'), 'id' => 'jigoshop_flat_rate2_handling_fee', 'std' => '');
			$defaults[] = array('name' => __('Method available for','jigoshop'), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_flat_rate2_availability', 'std' => 'all', 'type' => 'select', 'choices' => array('all' => __('All allowed countries', 'jigoshop'), 'specific' => __('Specific Countries', 'jigoshop')));
			$defaults[] = array('name' => __('Specific Countries','jigoshop'), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_flat_rate2_countries', 'std' => '', 'type' => 'multi_select_countries');
			return $defaults;
			}
		public function calculate_shipping() {
			$_tax = $this->get_tax();
			$this->shipping_total 	= 0;
			$this->shipping_tax 	= 0;
	    	if ($this->type=='order') :
			$this->shipping_total = $this->cost + $this->get_fee($this->fee, jigoshop_cart::$cart_contents_total);
            $this->shipping_total = ($this->shipping_total < 0 ? 0 : $this->shipping_total);
			if(Jigoshop_Base::get_options()->get_option('jigoshop_calc_taxes')=='yes' && $this->tax_status=='taxable') :
				$this->shipping_tax = $this->calculate_shipping_tax($this->shipping_total - jigoshop_cart::get_cart_discount_leftover());
			endif;
			else :
				if(sizeof(jigoshop_cart::$cart_contents)>0) :
					foreach (jigoshop_cart::$cart_contents as $item_id => $values) :
						$_product = $values['data'];
						if($_product->exists() && $values['quantity']>0 && $_product->product_type <> 'downloadable') :
							$item_shipping_price = ($this->cost + $this->get_fee($this->fee, $_product->get_price())) * $values['quantity'];
							$this->shipping_total = $this->shipping_total + $item_shipping_price;
							if($_product->is_shipping_taxable() && $this->tax_status=='taxable') :
								$_tax->calculate_shipping_tax($item_shipping_price, $this->id, $_product->get_tax_classes());
							endif;
						endif;
					endforeach;
					$this->shipping_tax = $_tax->get_total_shipping_tax_amount();
				endif;
			endif;
			}
		public function admin_scripts() {
			?>
			<script type="text/javascript">
			/*<![CDATA[*/
			jQuery(function($) {
				jQuery('select#jigoshop_flat_rate2_availability').change(function() {
					if(jQuery(this).val()=="specific") { jQuery(this).parent().parent().next('tr').show(); }
					else { jQuery(this).parent().parent().next('tr').hide(); }
					}).change();
				});
			/*]]>*/
			</script>
			<?php
			}
		}
	}
